﻿using System;
using System.Linq;
using Framework.Repository.Testing;
using Framework.Test.Common.Entities;
using Xunit;

namespace Framework.Tests.Repository
{
    public class AutoMappingDogTest
    {
        [Fact]
        public void Test_AutoMappingDogInMemory_Succeed()
        {
            using (var integrationTest = new IntegrationTest(new SqlitePersistence(), new AutoMappingTypeProvider().AddEntity<Cat>()))
            {
                var repository = integrationTest.CreateRepository<Cat>();
                const string minou = "Minou";
                const string minette = "Minette";
                var cat = new Cat { Name = minou, Tatoo = "ALC123" };
                var cat2 = new Cat { Name = minette, Tatoo = "ALC123" };

                repository.Save(cat, cat2);

                VerifyThatSameConnectionIsKeptBeetween2Sessions();

                Assert.NotNull(repository.Where(x => x.Name == minou).First());

                Assert.Equal(2, repository.Where(x => x.Name == minou || x.Name == minette).Count());

                repository.Delete(x => x.Name == minou || x.Name == minette);

                Assert.Equal(0, repository.Where(x => x.Name == minou || x.Name == minette).Count());
                Assert.False(repository.ToList().Any());
            }
        }

        private static void VerifyThatSameConnectionIsKeptBeetween2Sessions()
        {
            GC.Collect();
        }
    }
}